package com.t26.web.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.t26.web.dto.req.AppReqDto;
import com.t26.web.entity.App_info;
import com.t26.web.service.App_infoService;
import com.t26.web.vo.Json;
import com.t26.web.vo.ResultData;
import com.t26.web.vo.ResultDataGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Whenlean
 * @since 2021-06-12
 */

@RestController
@RequestMapping("/app_info")
public class App_infoController extends BaseController {

    @Autowired
    App_infoService app_infoService;

    /**
     *多条件查询手游应用
     * @param appReqDto
     * @return
     */
    @PostMapping("/gameAllInfo")
    public ResultData<List<App_info>> gameAllInfo(@RequestBody AppReqDto appReqDto){
        String softwareName = appReqDto.getSoftwareName();
        Long flatformId = appReqDto.getFlatformId();
        Long categoryLevel1 = appReqDto.getCategoryLevel1();
        Long categoryLevel2 = appReqDto.getCategoryLevel2();
        Long categoryLevel3 = appReqDto.getCategoryLevel3();

        QueryWrapper<App_info> wrapper  = new QueryWrapper<>();
        if(null != softwareName){
            wrapper.like("softwareName",softwareName);
        }
        if(null != flatformId){
            wrapper.eq("flatformId",flatformId);
        }
        if(null != categoryLevel3){
            wrapper.eq("categoryLevel3",categoryLevel3);
        }
        int current = 1;
        int size = 10;
        if (appReqDto.getCurrent() != null) current = appReqDto.getCurrent();
        if (appReqDto.getSize() != null) size = appReqDto.getSize();

        Page<App_info> page = app_infoService.page(new Page<>(current, size), wrapper);
        List<App_info> list = page.getRecords();
        list.forEach(app_info -> System.out.println("================="+app_info));
        return ResultDataGenerator.genSuccessResult(list,page,"chengg");
    }

    @PostMapping("/add")
    public ResultData add(@RequestBody App_info appInfo){

        Boolean flag = app_infoService.save(appInfo);
        if (!flag) {
            return ResultDataGenerator.genExceptionResult(flag,"插入失败");
        }
        return ResultDataGenerator.genSuccessResult(flag,"插入成功");
    }

    @PostMapping("update")
    public ResultData update(@RequestBody App_info appInfo){

        UpdateWrapper<App_info> wrapper = new UpdateWrapper<App_info>();
        wrapper.eq("id",appInfo.getId());
        Boolean flag = app_infoService.update(appInfo,wrapper);
        if (!flag){
            return ResultDataGenerator.genExceptionResult(flag,"更新失败");
        }
        return ResultDataGenerator.genSuccessResult(flag,"更新成功");
    }


    /**
     * 查询单个游戏的信息
     * @param id
     * @return
     */
    @GetMapping("/gameInfo")
    public ResultData<App_info> gameInfo(@RequestParam Long id,@RequestParam(required = false) Long status){
        if(null == id){
            return  ResultDataGenerator.genFailResult(null,"请输入id");
        }
        App_info app_info = app_infoService.getById(id);
        if(null != status){
            UpdateWrapper<App_info> wrapper = new UpdateWrapper<>();
            wrapper.eq("id",id);
            app_info.setStatus(status);
            app_infoService.update(app_info,wrapper);
        }

        return  ResultDataGenerator.genSuccessResult(app_info,"查询成功");
    }

    /*@GetMapping("/gameCheck")
    public Json gameCheck(@RequestParam Long status,@RequestParam Long id){
        UpdateWrapper<App_info> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);


        return Json.succ("");
    }*/

}

